package com;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ecbt.rest.util.ExcelHelper;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

public class TestUBRFiles {

	public static void main(String[] args) {
		List<List> resultList = new ArrayList<List>();
		try {
			resultList = ExcelHelper.readExcel2List(new File("D:\\AijunTao\\Desktop\\UBR\\URB-FILES.xlsx"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		List<UBRFile> ubrFileList = new ArrayList<>();
		for(int i = 1;i < resultList.size();i++){
			List one = (List)resultList.get(i);
			UBRFile ubrFile = UBRFile.builder()
					.ubrId(formatColumData(one.get(0)))
					.workNo(formatColumData(one.get(1)))
					.realName(formatColumData(one.get(2)))
					.readName(formatColumData(one.get(3)))
					.build();
			ubrFileList.add(ubrFile);
		}
		 Map<String, List<UBRFile>> ubrIdMap = ubrFileList.stream().collect(Collectors.groupingBy(UBRFile::getUbrId));
//		Map<String, String> realAndReadMap = ubrFileList.stream().collect(Collectors.toMap(UBRFile::getRealName, UBRFile::getReadName));
//		for(String key : realAndReadMap.keySet()) {
//			System.out.println(key + " " +realAndReadMap.get(key));
//		}
//		readFile("D:\\AijunTao\\Desktop\\UBR\\CARGO", realAndReadMap);
		changeUbrId("D:\\AijunTao\\Desktop\\UBR\\CARGO-ALERT", ubrIdMap);
	}
	
	private static void changeUbrId(String path, Map<String, List<UBRFile>> ubrIdMap) {
		File file = new File(path);
		File[] files = file.listFiles();
		for(File f : files) {
			if(ubrIdMap.containsKey(f.getName())) {
				System.out.println("----"+f.getAbsolutePath() + " --- " + f.getParent() + " ---- " + f.getName() + " -- " +ubrIdMap.get(f.getName()).get(0).getWorkNo());
				f.renameTo(new File(f.getParent() + File.separator + ubrIdMap.get(f.getName()).get(0).getWorkNo()));
			}else {
				System.out.println("----"+f.getAbsolutePath() + " --- " + f.getParent() + " ---- " + f.getName() + " -- ");
			}
		}
		
	}

	private static void readFile(String path, Map<String, String> realAndReadMap) {
		File file = new File(path);
		File[] files = file.listFiles();
		for(File f : files) {
			if(!f.isFile()) {
				System.out.println(f.getAbsolutePath());
				readFile(f.getPath(), realAndReadMap);
			}else {
				System.out.println("----"+f.getAbsolutePath() + " --- " + f.getParent() + " ---- " + f.getName() + " -- " +realAndReadMap.get(f.getName()));
				if(realAndReadMap.containsKey(f.getName())) {
					f.renameTo(new File(f.getParent() + File.separator + realAndReadMap.get(f.getName())));
				}
			}
		}
		
	}

	public static String formatColumData(Object object) {
		if(object != null)
			return object.toString();
		return null;
	}
	
	@Data
	@AllArgsConstructor
	@NoArgsConstructor
	@Builder
	static class UBRFile {
		private String ubrId;
		private String workNo;
		private String realName;
		private String readName;
	}
}
